package com.todod.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.todod.common.util.SnowflakeIdGenerator;
import com.todod.entity.QueryProjectInfo;
import com.todod.mapper.DataDictoryFileMapper;
import com.todod.model.DataDictoryFile;
import com.todod.service.IDataDictoryFileService;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author dyl
 * @date 2022-10-05
 */
@Service
@Transactional
public class DataDictoryFileServiceImpl extends ServiceImpl<DataDictoryFileMapper, DataDictoryFile>
		implements IDataDictoryFileService {
	@Autowired
	private DataDictoryFileMapper dataDictoryFileMapper;

	@Override
	public IPage<DataDictoryFile> getListPage(Long _userId, QueryProjectInfo queryProjectInfo) {
		Page<DataDictoryFile> page = new Page<>(queryProjectInfo.getPage(), queryProjectInfo.getLimit());

		IPage<DataDictoryFile> iPage = dataDictoryFileMapper.getListPage(page, _userId, queryProjectInfo);

		return iPage;
	}

	@Override
	public void saveData(Long _userId, DataDictoryFile dataDictoryFile) {

		dataDictoryFile.setId(SnowflakeIdGenerator.getId());
		dataDictoryFile.setCode(String.valueOf(SnowflakeIdGenerator.getId()));
		dataDictoryFile.setVersion(1);

		dataDictoryFile.setIsNew(1l);
//		设置删除标识为1（默认：为1）
		dataDictoryFile.setFlag(1);
		dataDictoryFile.setCreateTime(LocalDateTime.now());
		dataDictoryFile.setCreateUserId(_userId);
		dataDictoryFileMapper.insert(dataDictoryFile);
	}

	@Override
	public void editData(Long _userId, DataDictoryFile dataDictoryFile) {

		DataDictoryFile dataDictoryFile1 = dataDictoryFileMapper.selectById(dataDictoryFile.getId());
		dataDictoryFile1.setIsNew(2l);

		dataDictoryFile1.setCreateUserId(dataDictoryFile1.getCreateUserId());
		dataDictoryFile1.setCreateTime(dataDictoryFile1.getCreateTime());
		dataDictoryFile1.setModifyUserId(_userId);
		dataDictoryFile1.setModifyTime(LocalDateTime.now());
		dataDictoryFileMapper.updateById(dataDictoryFile1);

		dataDictoryFile.setId(SnowflakeIdGenerator.getId());
		dataDictoryFile.setVersion(dataDictoryFile1.getVersion() + 1);
		dataDictoryFile.setCode(dataDictoryFile1.getCode());
//		设置删除标识为1（默认：为1）
		dataDictoryFile.setFlag(1);
		dataDictoryFile.setIsNew(1l);

		dataDictoryFile.setCreateUserId(_userId);
		dataDictoryFile.setCreateTime(LocalDateTime.now());
		dataDictoryFileMapper.insert(dataDictoryFile);
	}

	@Override
	public List<DataDictoryFile> getListByCode(String code) {

		List<DataDictoryFile> list = dataDictoryFileMapper.getListByCode(code);

		return list;
	}

	@Override
	public List<DataDictoryFile> getInfo(String code, String isNew) {
		List<DataDictoryFile> list = dataDictoryFileMapper.getInfo(code, isNew);

		return list;
	}

	@Override
	public List<DataDictoryFile> getFileList(Long _userId, Long companyId) {
		List<DataDictoryFile> list = dataDictoryFileMapper.getFileList(_userId, companyId);

		return list;
	}

	@Override
	public List<DataDictoryFile> getListByXmId(String xmId) {
		List<DataDictoryFile> list = dataDictoryFileMapper.getListByXmId(xmId);

		return list;
	}

	@Override
	public List<DataDictoryFile> getListByClassifyId(Long classifyId) {
		QueryWrapper<DataDictoryFile> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("dmf_classify_id", classifyId);
		queryWrapper.eq("dmf_is_new", 1L);
		queryWrapper.eq("dmf_flag", 1L);
		queryWrapper.orderByDesc("dmf_sort_num");

		List<DataDictoryFile> list = dataDictoryFileMapper.selectList(queryWrapper);

		return list;
	}

	@Override
	public List<DataDictoryFile> getListByXftId(Long code) {
		QueryWrapper<DataDictoryFile> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("dmf_classify_id", code);
		queryWrapper.orderByDesc("dmf_sort_num");

		List<DataDictoryFile> list = dataDictoryFileMapper.selectList(queryWrapper);

		return list;
	}

}
